(function(c) {
    var d = mobiscroll,
        a = d.$,
        b = d.util,
        e = b.isString,
        f = {
            inputClass: '',
            invalid: [],
            rtl: false,
            showInput: true,
            groupLabel: 'Groups',
            checkIcon: 'checkmark',
            dataText: 'text',
            dataValue: 'value',
            dataGroup: 'group',
            dataDisabled: 'disabled'
        };
    d.presetShort('select');
    d.presets.scroller.select = function(g) {
        var r, K, n, A, p, l, k, m, j, h, y, E = 1000,
            i = a(this),
            J = a.extend({}, g.settings),
            d = a.extend(g.settings, f, J),
            O = d.readonly,
            G = d.layout || (/top|bottom/.test(d.display) ? 'liquid' : ''),
            D = G == 'liquid',
            q = b.isNumeric(d.select) ? d.select : d.select == 'multiple' || i.prop('multiple'),
            x = this.id + '_dummy',
            C = a('label[for="' + this.id + '"]').attr('for', x),
            H = d.label !== c ? d.label : C.length ? C.text() : i.attr('name'),
            B = !!d.data,
            w = B ? !!d.group : a('optgroup', i).length,
            s = d.group,
            o = w && s && s.groupWheel !== false,
            u = w && s && o && s.clustered === true,
            I = w && (!s || s.header !== false && !u),
            v = i.val() || [],
            t = [];

        function L() {
            var f, g, b, n, e, k = 0,
                h = 0,
                l = {};
            j = {};
            p = {};
            m = [];
            A = [];
            t.length = 0;
            if (B) {
                a.each(d.data, function(i, a) {
                    n = a[d.dataText];
                    e = a[d.dataValue];
                    g = a[d.dataGroup];
                    b = {
                        value: e,
                        text: n,
                        index: i
                    };
                    j[e] = b;
                    m.push(b);
                    if (w) {
                        if (l[g] === c) {
                            f = {
                                text: g,
                                value: h,
                                options: [],
                                index: h
                            };
                            p[h] = f;
                            l[g] = h;
                            A.push(f);
                            h++;
                        } else {
                            f = p[l[g]];
                        }
                        if (u) {
                            b.index = f.options.length;
                        }
                        b.group = l[g];
                        f.options.push(b);
                    }
                    if (a[d.dataDisabled]) {
                        t.push(e);
                    }
                });
            } else {
                if (w) {
                    a('optgroup', i).each(function(c) {
                        p[c] = {
                            text: this.label,
                            value: c,
                            options: [],
                            index: c
                        };
                        A.push(p[c]);
                        a('option', this).each(function(a) {
                            b = {
                                value: this.value,
                                text: this.text,
                                index: u ? a : k++,
                                group: c
                            };
                            j[this.value] = b;
                            m.push(b);
                            p[c].options.push(b);
                            if (this.disabled) {
                                t.push(this.value);
                            }
                        });
                    });
                } else {
                    a('option', i).each(function(a) {
                        b = {
                            value: this.value,
                            text: this.text,
                            index: a
                        };
                        j[this.value] = b;
                        m.push(b);
                        if (this.disabled) {
                            t.push(this.value);
                        }
                    });
                }
            }
            if (m.length) {
                K = m[0].value;
            }
            if (I) {
                m = [];
                k = 0;
                a.each(p, function(c, d) {
                    e = '__group' + c;
                    b = {
                        text: d.text,
                        value: e,
                        group: c,
                        index: k++,
                        cssClass: 'mbsc-sel-gr'
                    };
                    j[e] = b;
                    m.push(b);
                    t.push(b.value);
                    a.each(d.options, function(b, a) {
                        a.index = k++;
                        m.push(a);
                    });
                });
            }
        }

        function M(b, d, e) {
            var a, c = [];
            for (a = 0; a < b.length; a++) {
                c.push({
                    value: b[a].value,
                    display: b[a].text,
                    cssClass: b[a].cssClass
                });
            }
            return {
                circular: false,
                multiple: d,
                data: c,
                label: e
            };
        }

        function N() {
            return M(A, false, d.groupLabel);
        }

        function F() {
            return M(u ? p[n].options : m, q, H);
        }

        function P() {
            var b, c, a = [
                []
            ];
            if (o) {
                b = N();
                if (D) {
                    a[0][l] = b;
                } else {
                    a[l] = [b];
                }
            }
            c = F();
            if (D) {
                a[0][h] = c;
            } else {
                a[h] = [c];
            }
            return a;
        }

        function z(b) {
            if (q) {
                if (b && e(b)) {
                    b = b.split(',');
                }
                if (a.isArray(b)) {
                    b = b[0];
                }
            }
            k = b === c || b === null || b === '' || !j[b] ? K : b;
            if (o) {
                n = j[k] ? j[k].group : null;
            }
        }

        function T(b, c) {
            var a = b ? g._tempWheelArray : g._hasValue ? g._wheelArray : null;
            return a ? d.group && c ? a : a[h] : null;
        }

        function S(d) {
            var a, b, c = [];
            if (q) {
                for (a in g._tempSelected[h]) {
                    c.push(j[a] ? j[a].text : '');
                }
                return c.join(', ');
            }
            b = d[h];
            return j[b] ? j[b].text : '';
        }

        function R() {
            var a = g.getVal(),
                b = g._tempValue;
            r.val(b);
            i.val(a);
        }

        function Q() {
            var a = {};
            a[h] = F();
            y = true;
            g.changeWheel(a);
        }
        g.setVal = function(a, d, f, c, i) {
            if (q) {
                if (a && e(a)) {
                    a = a.split(',');
                }
                g._tempSelected[h] = b.arrayToObject(a);
                if (!c) {
                    g._selected[h] = b.arrayToObject(a);
                }
                a = a ? a[0] : null;
            }
            g._setVal(a, d, f, c, i);
        };
        g.getVal = function(a, c) {
            if (q) {
                return b.objectToArray(a ? g._tempSelected[h] : g._selected[h]);
            }
            return T(a, c);
        };
        g.refresh = function() {
            var a = {};
            L();
            d.wheels = P();
            z(k);
            a[h] = F();
            g._tempWheelArray[h] = k;
            if (o) {
                a[l] = N();
                g._tempWheelArray[l] = n;
            }
            if (g._isVisible) {
                g.changeWheel(a, 0, true);
            }
        };
        if (!d.invalid.length) {
            d.invalid = t;
        }
        if (o) {
            l = 0;
            h = 1;
        } else {
            l = -1;
            h = 0;
        }
        if (q) {
            i.prop('multiple', true);
            g._selected[h] = {};
            if (v && e(v)) {
                v = v.split(',');
            }
            g._selected[h] = b.arrayToObject(v);
        }
        a('#' + x).remove();
        if (i.next().is('input.mbsc-control')) {
            r = i.off('.mbsc-form').next().removeAttr('tabindex');
        } else {
            r = a('<input type="text" id="' + x + '" class="mbsc-control mbsc-control-ev ' + d.inputClass + '" readonly />');
            if (d.showInput) {
                r.insertBefore(i);
            }
        }
        g.attachShow(r.attr('placeholder', d.placeholder || ''));
        i.addClass('mbsc-sel-hdn').attr('tabindex', -1);
        L();
        z(i.val());
        return {
            layout: G,
            headerText: false,
            anchor: r,
            compClass: 'mbsc-sel' + (o ? ' mbsc-sel-gr-whl' : '') + (q ? ' mbsc-sel-multi' : ''),
            setOnTap: o ? [false, true] : true,
            formatValue: S,
            parseValue: function(a) {
                z(a === c ? i.val() : a);
                return o ? [n, k] : [k];
            },
            validate: function(e) {
                var b = e.index,
                    a = [];
                a[h] = d.invalid;
                if (u && !y && b === c) {
                    Q();
                }
                y = false;
                return {
                    disabled: a
                };
            },
            onRead: R,
            onFill: R,
            onBeforeShow: function() {
                if (q && d.counter) {
                    d.headerText = function() {
                        var b = 0;
                        a.each(g._tempSelected[h], function() {
                            b++;
                        });
                        return (b > 1 ? d.selectedPluralText || d.selectedText : d.selectedText).replace(/{count}/, b);
                    };
                }
                z(i.val());
                g.settings.wheels = P();
                y = true;
            },
            onWheelGestureStart: function(a) {
                if (a.index == l) {
                    d.readonly = [false, true];
                }
            },
            onWheelAnimationEnd: function(b) {
                var a = g.getArrayVal(true);
                if (b.index == l) {
                    d.readonly = O;
                    if (a[l] != n) {
                        n = a[l];
                        k = p[n].options[0].value;
                        a[h] = k;
                        if (u) {
                            Q();
                        } else {
                            g.setArrayVal(a, false, false, true, E);
                        }
                    }
                } else if (b.index == h && a[h] != k) {
                    k = a[h];
                    if (o && j[k].group != n) {
                        n = j[k].group;
                        a[l] = n;
                        g.setArrayVal(a, false, false, true, E);
                    }
                }
            },
            onDestroy: function() {
                if (!r.hasClass('mbsc-control')) {
                    r.remove();
                }
                i.removeClass('mbsc-sel-hdn').removeAttr('tabindex');
            }
        };
    };
}());